package pl.edu.agh.iosr.gamblingzone.adminpanel.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.dao.MoneyVoucherRequestDAO;
import pl.edu.agh.iosr.gamblingzone.model.BettingSlip;
import pl.edu.agh.iosr.gamblingzone.model.MoneyVoucher;
import pl.edu.agh.iosr.gamblingzone.model.MoneyVoucherRequest;
import pl.edu.agh.iosr.gamblingzone.model.User;
import pl.edu.agh.iosr.gamblingzone.utils.DAOFactory;
import pl.edu.agh.iosr.gamblingzone.utils.EmailUtils;
import pl.edu.agh.iosr.gamblingzone.utils.HashGenerator;

@Stateful
@Scope(ScopeType.SESSION)
@Name("voucherManage")
public class MoneyVouchersManageAction implements MoneyVouchersManage {
	
	@PersistenceContext
	private EntityManager em;
	
	@Logger
	Log log;
	
	@In(required=true)
	DAOFactory dao;
	
	@In
	private FacesMessages facesMessages;
	
	private MoneyVoucherRequestDAO voucherReqDAO;
	
	@DataModel
	private List<MoneyVoucherRequest> voucherRequests;
	
	@DataModelSelection
	@Out(required = false)
	private MoneyVoucherRequest vr;
	
	@Create
	public void init() {
		voucherReqDAO = dao.getVoucherRequestDAO();
	}
		
	public List<MoneyVoucherRequest> refreshVoucherRequests() {		
		voucherRequests = voucherReqDAO.findAll();		
		return voucherRequests;
	}
	
	public void accept(MoneyVoucherRequest voucherRequest) {
		voucherRequest = em.merge(voucherRequest);
		
		User user = voucherRequest.getOwner();
		MoneyVoucher voucher = new MoneyVoucher();
		
		voucher.setActive(Boolean.TRUE);
		voucher.setValue(voucherRequest.getMoney());
		
		String voucherHash = HashGenerator.generateVoucherHash();
		voucher.setVoucherHash(voucherHash);
		
		Calendar expirationDate = Calendar.getInstance();
		expirationDate.add(Calendar.DATE, 1);
		voucher.setExpirationDate(expirationDate.getTime());
		
		user.addVouchers(voucher);
		sendEmailToUser(user, voucherHash);
		voucherReqDAO.makeTransient(voucherRequest);
		
		log.info("Voucher hash " + voucher.getVoucherHash());
		facesMessages.add("Request was accepted, click button to refresh");
	}
	
	public void decline(MoneyVoucherRequest voucherRequest) {
		voucherRequest = em.merge(voucherRequest);
		voucherReqDAO.makeTransient(voucherRequest);		
		
		log.info("Voucher " + voucherRequest.getId() + "declined");
		facesMessages.add("Request was declined, click button to refresh");
	}
	
	private boolean sendEmailToUser(User user, String hash){
		boolean result = true;
		
		Email email = new SimpleEmail();
		
		try {
			email.setHostName(EmailUtils.SMTP_SERVER);			
			email.addTo(user.getEmail(), user.getFirstName() + " " + user.getSurname());
			email.setFrom(EmailUtils.FROM_ADDRESS, EmailUtils.FROM_NAME);
			email.setSubject(EmailUtils.SUBJECT_VOUCHER);
			email.setMsg(EmailUtils.BODY_VOUCHER + hash);
			//Commented in order not to spam
			//email.send();
		} catch (EmailException e) {
			log.error("Email exception while sending voucher", e);
			result = false;
		}
		
		return result;
	}
	
	@Remove
	public void destroy() {
	}
}
